package bll

import (
	"context"
	"manager/event"
	"manager/model"
	"manager/model/entity"
	"manager/store/postgres"
	"manager/store/redis"
	"time"
)

const (
	RedisKeyAlarmType = "alarm_type"
)

var CacheAlarmType = &cacheAlarmType{}

// cacheBikeType 缓存车辆类型
type cacheAlarmType struct {
}

func (c *cacheAlarmType) init() func() {
	return func() {}
}

func (c *cacheAlarmType) onEvent(data *event.Data) {}

// GetAlarmType 获取警告类型
func (c *cacheAlarmType) GetAlarmType(code string) (info *entity.AlarmType, err error) {
	// 从缓存中获取车辆类型
	info = &entity.AlarmType{}
	redis.KV.GetValue(context.Background(), RedisKeyAlarmType, code, info)
	if info.Id > 0 {
		return
	}

	// 获取告警类型
	info, err = postgres.AlarmType.Find(context.Background(), &model.AlarmTypeInfoRequest{Code: code})
	if err != nil {
		return
	}

	// 设置缓存
	redis.KV.AddValueWithExpireTime(context.Background(), RedisKeyAlarmType, code, info, time.Second*600)
	return
}

// DeleteCache 删除缓存
func (c *cacheAlarmType) DeleteCache(code string) (err error) {
	return redis.KV.DelValue(context.Background(), RedisKeyAlarmType, code)
}
